using Function.SkillEditor.Runtime;
using SCore.Editor.WindowDraw;
using SCore.Function.Fixed;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public class SkillTriggerClipDraw : ISkillClipDrawer
    {
        private int _selectIndex;
        private ShapeDrawerManager _shapeDrawerManager;

        private void AddDrawAttackBind()
        {
            var clip = _skillClip as SkillTriggerClip;
            if (_shapeDrawerManager == null)
            {
                _shapeDrawerManager = GameObject.FindObjectOfType<ShapeDrawerManager>();
                if (_shapeDrawerManager == null)
                {
                    _shapeDrawerManager = GameObject.Find("Editor Camera").GetOrAddComponent<ShapeDrawerManager>();
                }
            }

            _shapeDrawerManager.AddClip(SkillTimeLineSubWindow._editorGo, clip);
        }

        private void RemoveDrawAttackBind()
        {
            var clip = _skillClip as SkillTriggerClip;
            if (_shapeDrawerManager != null)
            {
                _shapeDrawerManager.RemoveClip(clip);
            }
        }

        protected override void ClipNameGUI()
        {
            var clip = (_skillClip as SkillTriggerClip);
            EditorGUI.LabelField(_rect, $"T:{clip._skillTriggerType}S:{clip._skillAttackShape}",
                EditorDrawHelp.GetTextGUIStyle(10, TextAnchor.MiddleCenter,
                    EditorDrawHelp._black, FontStyle.Bold));
        }

        public override void OnInspector(Rect rect)
        {
            base.OnInspector(rect);
            var clip = (_skillClip as SkillTriggerClip);

            EditorGUI.BeginChangeCheck();
            var bPointRect = new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20);
            GUI.Label(new Rect(bPointRect.x, bPointRect.y, bPointRect.width * 0.3f, bPointRect.height), "类型");
            clip._skillTriggerType = (SkillTriggerType)EditorGUI.EnumPopup(
                new Rect(bPointRect.x + bPointRect.width * 0.3f, bPointRect.y, bPointRect.width * 0.7f,
                    bPointRect.height),
                clip._skillTriggerType);
            if (EditorGUI.EndChangeCheck())
            {
                if (clip._skillTriggerType != SkillTriggerType.Shape)
                {
                    clip._skillAttackShape = SkillAttackShape.None;
                    clip._pos = fp3.zero;
                    clip._param1 = fp.Zero;
                    clip._param2 = fp.Zero;
                    clip._param3 = fp.Zero;
                    clip._param4 = fp.Zero;
                }
            }

            SkillAttackShape skillAttackShape = clip._skillAttackShape;
            int selectIndex = _selectIndex;
            fp param1 = clip._param1;
            fp param2 = clip._param2;
            fp param3 = clip._param3;
            Vector3 pos = Vector3.zero;
            if (clip._skillTriggerType == SkillTriggerType.Shape)
            {
                pos = EditorDrawHelp.DrawVector3Field(
                    new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20),
                    "偏移",
                    clip._pos.ToVector3());

                bPointRect = new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20);
                GUI.Label(new Rect(bPointRect.x, bPointRect.y, bPointRect.width * 0.3f, bPointRect.height), "形状");
                skillAttackShape = (SkillAttackShape)EditorGUI.EnumPopup(
                    new Rect(bPointRect.x + bPointRect.width * 0.3f, bPointRect.y, bPointRect.width * 0.7f,
                        bPointRect.height),
                    clip._skillAttackShape);
                _selectIndex = EditorDrawHelp.DrawPopupField(
                    new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20),
                    "挂点", _selectIndex, SkillEditorConfig.Instance.GetBindContent());
                if (skillAttackShape == SkillAttackShape.Box)
                {
                    var lastWidth = EditorGUIUtility.labelWidth;
                    EditorGUIUtility.labelWidth = 50;
                    param1 = EditorGUI.FloatField(
                        new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), new GUIContent("长"),
                        clip._param1.AsFloat());
                    param2 = EditorGUI.FloatField(
                        new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), new GUIContent("宽"),
                        clip._param2.AsFloat());
                    param3 = EditorGUI.FloatField(
                        new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), new GUIContent("高"),
                        clip._param3.AsFloat());
                    EditorGUIUtility.labelWidth = lastWidth;
                }

                if (GUI.Button(new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), "定位挂点"))
                {
                    var bindName = SkillEditorConfig.Instance.GetBindContent()[_selectIndex].text;
                    if (SkillTimeLineSubWindow._editorGo != null)
                    {
                        pos = SkillTimeLineSubWindow._editorGo.transform.DeepFindChild(bindName).position -
                              SkillTimeLineSubWindow._editorGo.transform.position;
                    }
                }
            }


            if (EditorGUI.EndChangeCheck())
            {
                if (clip._skillTriggerType == SkillTriggerType.Shape)
                {
                    clip._pos = new fp3(pos);
                    clip._skillAttackShape = skillAttackShape;
                    clip._param1 = param1;
                    clip._param2 = param2;
                    clip._param3 = param3;
                }
            }
        }

        public override void OnProgress(bool isPlay, fp progress)
        {
            AddDrawAttackBind();
        }

        public override void OnExit(bool isPlay, fp progress)
        {
            RemoveDrawAttackBind();
        }

        public override void OnDisable()
        {
            RemoveDrawAttackBind();
        }
    }


    [TrackDrawEditor(typeof(SkillTriggerTrack), typeof(SkillTriggerClipDraw))]
    public class SkillTriggerTrackDraw : ISkillTrackDrawer<SkillTriggerTrack>
    {
        public SkillTriggerTrackDraw(SkillTriggerTrack skillTrack, TrackRegisterAttribute trackRegister) : base(
            skillTrack, trackRegister)
        {
        }

        protected override void DrawGroup()
        {
            EditorGUI.LabelField(new Rect(_headRect.x + 10, _headRect.y, _headRect.width, _headRect.height), "Trigger");
        }
    }
}